using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Params;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Mvc;
using ArticleManagementSystem.Api.Db;
using Microsoft.EntityFrameworkCore;
using ArticleManagementSystem.Api.Utils;

namespace ArticleManagementSystem.Api.Controllers
{
    [ApiController]
    [Route("[Controller]")]
    public class RecommendController : ControllerBase
    {
        private readonly IRepostisory<Article> _articleRepostisory;
        private readonly IRepostisory<Recommend> _recommendRepostisory;
        private readonly IRepostisory<AuditInfo> _auditInfoRepostisory;
        private readonly IRepostisory<Comment> _commentRepostisory;
        private readonly DataBase _context;

        public RecommendController(DataBase context, IRepostisory<Comment> commentRepostisory, IRepostisory<AuditInfo> auditInfoRepostisory, IRepostisory<Article> articleRepostisory, IRepostisory<Recommend> recommendRepostisory)
        {
            _articleRepostisory = articleRepostisory;
            _recommendRepostisory = recommendRepostisory;
            _auditInfoRepostisory = auditInfoRepostisory;
            _commentRepostisory = commentRepostisory;
            _context = context;

        }

        //管理员添加推荐文章
        [HttpPost, Route("EditRecommend")]
        public dynamic EditRecommend(EditRecommend edit)
        {
            var adminid = edit.AdminId;
            var aricleid = edit.ArticleId;

            var isadd = _recommendRepostisory.Table.Where(x => x.AricleId == aricleid).FirstOrDefault();
            if (isadd != null)
            {
                var id = isadd.Id;
                _recommendRepostisory.Deleted(id);
                return new
                {
                    code = 200,
                    data = false,
                    msg = "取消推荐"
                };
            }else{
                 var res = new Recommend
            {
                AdminId = adminid,
                AricleId = aricleid
            };

            _recommendRepostisory.Insert(res);

            return new
            {
                code = 200,
                data = true,
                list = res,
                msg = "设为推荐"
            };
            }
           
        }


        //获取管理员推荐
        [HttpGet, Route("AdminRecommend")]
        public dynamic AdminRecommend()
        {
            var RecommendTime = DateTime.Now.AddDays(-10);
            var Table = _recommendRepostisory.Table;
            var AdminRecommendMsg = Table.Where(x => x.CreatedTime > RecommendTime).ToList();
            ArrayList reslist = new ArrayList();
            foreach (var item in AdminRecommendMsg)
            {
                var ArticleMsg = _articleRepostisory.Table.Where(x => x.Id == item.AricleId).FirstOrDefault();
                if (ArticleMsg != null)
                {
                    reslist.Add(ArticleMsg);
                }
            }
            return new
            {
                code = 200,
                data = reslist,
                msg = "获取成功"
            };
        }

        /// <summary>
        /// 浏览量文章推荐
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("BrowseRecommend")]
        public dynamic BrowseRecommend()
        {
            var RecommendTime = DateTime.Now.AddDays(-2);
            var Pageviwes = _context.Pageviwes.Include(Pageviwes => Pageviwes.Articles).AsSplitQuery().ToList();
            var groupByMsg = Pageviwes.Where(x => x.CreatedTime > RecommendTime).GroupBy(p => new { p.ArticleId })
                    .Select(p => new { ArticleId = p.Key.ArticleId, count = p.Sum(s => 1) }).ToList();

            ArrayList ArticleMsg = new ArrayList();
            foreach (var item in groupByMsg)
            {
                ArticleMsg.Add(item.ArticleId);
            }
            var res = ArticleSelectHelper.GetArticleMsg(ArticleMsg, _context);
            return new
            {
                code = 200,
                data = res,
                msg = "获取成功"
            };
        }

        /// <summary>
        /// 热评文章推荐
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("CommentRecommend")]
        public dynamic CommentRecommend()
        {
            // var ArticleId = msg.ArticleId;
            var RecommendTime = DateTime.Now.AddDays(-2);
            var comment = _commentRepostisory.Table;
            var groupByMsg = comment.Where(x => x.CreatedTime > RecommendTime).GroupBy(p => new { p.ArticleId })
                    .Select(p => new { ArticleId = p.Key.ArticleId, count = p.Sum(s => 1) }).OrderByDescending(x => x.count).ToList();
            ArrayList ArticleMsg = new ArrayList();
            foreach (var item in groupByMsg)
            {
                // ArticleMsg.Add(item.ArticleId);
                var commentRecommendArticle = _context.article.Where(x => x.Id == item.ArticleId).FirstOrDefault();
                if (commentRecommendArticle != null)
                {
                    ArticleMsg.Add(commentRecommendArticle);
                }
            }
            // var res = ArticleSelectHelper.GetArticleMsg(ArticleMsg, _context);
            return new
            {
                code = 200,
                data = ArticleMsg,
                count = groupByMsg,
                msg = "获取成功"
            };
        }

        /// <summary>
        /// 获取审计日志
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("AuditInfo")]
        public dynamic GetAuditInfo()
        {
            var table = _auditInfoRepostisory.Table;
            return new
            {
                table
            };
        }

    }
}